﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using Comm;

namespace WCFFileTransfer
{
    public class FileTransfer: IFileTransfer
    {
        /// <summary>
        /// 传送文件
        /// </summary>
        /// <param name="file"></param>
        public void TransferFile(FileWrapper file)
        {
            // 获取配置文件中的路径
            string configpath = ConfigSet.GetValue("SavePath");
            if (!configpath.EndsWith("\\"))
                configpath += "\\";
            file.FilePath = configpath + file.FilePath;
            // 不存在则创建目录
            if (!Directory.Exists(file.FilePath))
                Directory.CreateDirectory(file.FilePath);
            var sourceStream = file.FileData;
            string pathname = Path.Combine(file.FilePath, file.FileName);
            var targetStream = new FileStream(pathname, FileMode.Create,
                FileAccess.Write, FileShare.None);
            var buffer = new byte[4096];
            var count = 0;
            while ((count = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                targetStream.Write(buffer, 0, count);
            targetStream.Close();
            sourceStream.Close();
        }
    }
}
